package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl implements BaseCategoryTrademarkService {

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    /**
     * 根据三级分类id 获取品牌列表
     * @param category3Id
     * @return
     */
    @Override

    public List<BaseTrademark> findTrademarkList(Long category3Id) {

        List<Long> trademarkIdList = trademarkIdList(category3Id);
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            //使用获取到的id list 查询品牌信息
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(trademarkIdList);

            return baseTrademarkList;
        }
        return null;
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {

        //查询所有的品牌信息
        List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null);

        if (!CollectionUtils.isEmpty(baseTrademarkList)){
            List<Long> trademarkIdList = trademarkIdList(category3Id);

            List<BaseTrademark> collect = baseTrademarkList.stream().filter(baseTrademark -> {
                return !trademarkIdList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());

            return collect;
        }
        return null;
    }

    /**
     * 添加分类的关联信息
     * @param categoryTrademarkVo
     */
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {

        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        if (!CollectionUtils.isEmpty(trademarkIdList)){
            trademarkIdList.stream().forEach(tmId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tmId);

                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);

            });
        }

    }

    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void remove(Long category3Id, Long trademarkId) {

        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category3_id",category3Id);
        queryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(queryWrapper);
    }

    // 抽取的公共方法 用于查询中间表分类id 对应的品牌id
    public List<Long> trademarkIdList(Long category3Id){
        //先查询中间表，获取品牌表id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        //使用stream流将trademark_id获取到
        List<Long> trademarkIdList = baseCategoryTrademarks.stream().map(baseCategoryTrademark -> {
            return baseCategoryTrademark.getTrademarkId();
        }).collect(Collectors.toList());

        return trademarkIdList;
    }
}
